package cn.wubo.moredb.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "db1EntityManagerFactory",
        basePackages = {"cn.wubo.moredb.db1.repository"}) //设置Repository所在位置
public class Db1DataSourceConfig {
    @Autowired
    @Qualifier("db1DataSource")
    private DataSource db1DataSource;

    @Primary
    @Bean(name = "db1EntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean icmEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(db1DataSource)
                .packages("cn.wubo.moredb.db1.entity") //设置实体类所在位置
                .persistenceUnit("db1PersistenceUnit")
                .build();
    }
}
